package com.kzn.util;

import com.kzn.entity.DetailedTask;
import com.kzn.entity.GlobalVar;
import com.kzn.entity.Rect;
import jdk.nashorn.internal.ir.IfNode;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class Dom4jUtil {
    public static Boolean parseRectToVOC(List<Rect> rects, DetailedTask detailedTask, Integer imageWidth, Integer imageHeight) {
        Document document = DocumentHelper.createDocument();
        Element annotation = document.addElement("annotation");
        Element folder = annotation.addElement("folder");
        folder.setText(detailedTask.getProjectName());
        Element fileName = annotation.addElement("fileName");
        fileName.setText(detailedTask.getSourceFilePath().trim().split("/")[1]);

        Element path = annotation.addElement("path");
        path.setText("../../image/" + detailedTask.getSourceFilePath());

        Element source = annotation.addElement("source");
        Element database = source.addElement("database");
        database.setText("Unspecified");

        Element size = annotation.addElement("size");
        Element width = size.addElement("width");
        Element height = size.addElement("height");
        Element depth = size.addElement("depth");
        width.setText(imageWidth.toString());
        height.setText(imageHeight.toString());
        depth.setText(String.valueOf(3));

        for (Rect rect : rects) {
            Element object = annotation.addElement("object");

            Element name = object.addElement("name");
            Element bndBox = object.addElement("bndbox");
            Element truncated = object.addElement("truncated");
            Element difficult = object.addElement("difficult");
            Element pose = object.addElement("pose");
            name.setText(rect.getName());
            truncated.setText("Unspecified");
            difficult.setText("Unspecified");
            pose.setText("Unspecified");


            Element xMin = bndBox.addElement("xmin");
            Element yMin = bndBox.addElement("ymin");
            Element xMax = bndBox.addElement("xmax");
            Element yMax = bndBox.addElement("ymax");
            xMin.setText(String.valueOf((int) (rect.getX() * imageWidth)));
            yMin.setText(String.valueOf((int) (rect.getY() * imageHeight)));
            xMax.setText(String.valueOf((int) (rect.getEx() * imageWidth)));
            yMax.setText(String.valueOf((int) (rect.getEy() * imageHeight)));
        }


        String resultPath = GlobalVar.UPLOAD_PATH + "/"
                + detailedTask.getProjectName()
                + "/result_xml/"
                + detailedTask.getSourceFilePath().split("\\.")[0]
                + ".xml";

        File result = new File(resultPath);
        String resultFolderPath = result.getParent();
        File resultFolder = new File(resultFolderPath);
        if (!resultFolder.exists()) {
            resultFolder.mkdirs();
        }
        document.getRootElement().asXML();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setSuppressDeclaration(true);
        format.setOmitEncoding(true);
        try {
            XMLWriter writer = new XMLWriter(new FileWriter(resultPath), format);
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;

    }

    public static Boolean parseVOCToCSV(DetailedTask detailedTask) {
        String VOCPath = GlobalVar.UPLOAD_PATH + "/" + detailedTask.getProjectName() + "/result_xml";
        File VOCFolder = new File(VOCPath);
        SAXReader reader = new SAXReader();

//        for (File file:VOCFolder.listFiles()){
//            Document document
//        }


        return null;
    }
}
